package cn.kgc.student.controller;

import cn.kgc.student.pojo.Student;
import cn.kgc.student.service.StudentService;
import cn.kgc.student.utils.Page;
import cn.kgc.student.vo.StudentParam;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import sun.security.provider.MD5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;

/**
* 描述：学生 控制器
* @author 魏耀
* @date 2019/07/22
*/
@RestController
@RequestMapping("/student")
public class StudentController{
	@Reference
	private StudentService studentService;
 	
 	@RequestMapping("/findById.json")
    public Student findById(Integer id){
        Student result = null;
        try {
            result = studentService.findById(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 描述：获取所有的Student对象
     */
    @RequestMapping("/findAll.json")
    public List<Student> findAll(){
        System.out.println("eeee");
        List<Student> result = null;
        try {
            result = studentService.findAll();
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 描述：新增保存Student对象
     */
    @RequestMapping("/save.json")
    public Map save(Student student){
        Map result = new HashMap();
        result.put("flag",false);
        result.put("msg","保存学生失败");
        try {
            if(studentService.save(student)>0){
                result.put("flag",true);
                result.put("msg","保存学生成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 描述：根据主键删除学生对象
     * @param id
     */
    @RequestMapping("/delete.json")
    public Map delete(Integer id){
        Map result = new HashMap();
        result.put("flag",false);
        result.put("msg","保存学生失败");
        try {
            if(studentService.delete(id)>0){
                result.put("flag",true);
                result.put("msg","保存学生成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 描述：更新学生对象
     */
    @RequestMapping("/update.json")
    public Map update(Student student){
        Map result = new HashMap();
        result.put("flag",false);
        result.put("msg","更新学生失败");
        try {
            if( studentService.update(student)>0){
                result.put("flag",true);
                result.put("msg","更新学生成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;

    }

    /**
     * 根据不确定的条件，动态查询
     */
    @RequestMapping("/query.json")
    public List<Student> query(StudentParam param){
        List<Student> result = null;
        try {
            result = studentService.query(param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 根据不确定的条件，动态分页查询
     */
    @RequestMapping("/queryByPage.json")
    public Page queryByPage(StudentParam param){
        Page result = null;
        try {
            result = studentService.queryByPage(param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }


 }
